# -* - coding: UTF-8 -* -
#import sys
#reload(sys)
#sys.setdefaultencoding('utf8')
from driver import ClientDriver
import requests,random
import os
import logging
#队列驱动抽像类
class HttpClient(ClientDriver):  
	def create(self):  
		self.head_connection = ['Keep-Alive','close']
		self.head_accept = ['text/html, application/xhtml+xml, */*']
		self.head_accept_language = ['zh-CN,fr-FR;q=0.5','en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3']
		self.head_user_agent = ['Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko',
						'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36',
						'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; rv:11.0) like Gecko)',
						'Mozilla/5.0 (Windows; U; Windows NT 5.2) Gecko/2008070208 Firefox/3.0.1',
						'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3',
						'Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070803 Firefox/1.5.0.12',
						'Opera/9.27 (Windows NT 5.2; U; zh-cn)',
						'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1Content-Type: application/x-www-form-urlencoded; charset=UTF-8',
						'Mozilla/5.0 (Macintosh; PPC Mac OS X; U; en) Opera 8.0',
						'Opera/8.0 (Macintosh; PPC Mac OS X; U; en)',
						'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.12) Gecko/20080219 Firefox/2.0.0.12 Navigator/9.0.0.6',
						'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0)',
						'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)',
						'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)',
						'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Maxthon/4.0.6.2000 Chrome/26.0.1410.43 Safari/537.1 ',
						'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E; QQBrowser/7.3.9825.400)',
						'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0 ',
						'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.92 Safari/537.1 LBBROWSER',
						'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0; BIDUBrowser 2.x)',
						'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/3.0 Safari/536.11']
		self.headers = {'Connection': 'keep-alive',
			'Accept':'application/json, text/javascript, */*; q=0.01',
			#'Origin': 'http://www.baidu.com',
			#'X-Requested-With': 'XMLHttpRequest',
			'User-Agent': self.head_user_agent[random.randrange(0,len(self.head_user_agent))],
			'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
			#'Referer': 'http://www.baidu.com/',
			'Accept-Language': 'zh-CN,zh;q=0.8',
			'Accept-Encoding': 'gzip, deflate',
			#'Cookie':head_cookie[random.randrange(0,len(head_cookie))]
		}		
	#发送请求
	def send(self,url,data=''):
		#logging.info(body)
		try:
			if url.startswith('post://'):
				r = requests.post(url, headers=self.headers,data=data)
			else:
				r = requests.get(url, headers=self.headers,params=data)
			return r.text
		except:
			logging.error("网络出错，3秒后将重试...")
			time.sleep(3)
		return False